package com.hnkywl.jip.action;

import com.hnkywl.jip.dao.VoucherType;
import com.hnkywl.jip.interceptor.AuthInterceptor;
import com.hnkywl.jip.service.BankLoanService;
import com.hnkywl.jip.service.VoucherService;
import com.hnkywl.jip.utils.UUIDGenerator;
import com.hnkywl.jip.utils.XmlParse;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-2-26
 * Time: 下午2:16
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/bank_loan")
public class BankLoanAction extends SimpleActionSupport {
    private Integer status;

    private String voucherNum;

    private Integer companyId;

    private Integer id;

    private Integer xxtype;

    private Integer attachment;

    private Integer limit;
    private Integer start;

    private Date systemDate;

    private Page<Object[]> gridResult=new Page<Object[]>();
    private Map userInfo;

    private Map<String, Object> voucherMerge;

    private Map<String, String> result = new HashMap<String, String>();

    private XmlParse xmlParse;

    private BankLoanService bankLoanService;

    private VoucherService voucherService;

    @Action("search")
    public String search() {
        return SUCCESS;
    }

    @Action("grid_json")
    public String gridJson() {
        gridResult = bankLoanService.findBankLoan(companyId, status, voucherNum, limit, start);
        return SUCCESS;
    }

    @Action("voucher_json")
    public String voucherJson() {
        systemDate = new Date();
        voucherMerge = bankLoanService.getVoucher(id,xxtype, null);
        userInfo = (HashMap<String, Object>) this.session.get(AuthInterceptor.SESSION_KEY);
        Map<String, Object> voucherMaster = bankLoanService.getVoucherMaster(id, attachment, null, null, null,userInfo);
        voucherMerge.put("master", voucherMaster);
        return SUCCESS;
    }

    @Action(value = "generate_voucher_json",
            results = {@Result(name = "success", location = "/WEB-INF/content/invoice_verify/response_json.ftl")})
    public String generateVoucher() {
        try {
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            Boolean begin =voucherService.beginGenerateVoucher(id, VoucherType.INVOICE_VERIFY);
              if (!begin)
            throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            //凭证分录
            voucherMerge = bankLoanService.getVoucher(id,xxtype, uuid);
            userInfo = (HashMap<String, Object>) this.session.get(AuthInterceptor.SESSION_KEY);
            //凭证头
            Map<String, Object> msgheader = voucherService.getMsgHeader(userInfo);
            voucherMerge.put("msgheader", msgheader);
            //凭证主表信息
            Map<String, Object> voucherMaster =
                    bankLoanService.getVoucherMaster(id, attachment, uuid, year.toString(), mouth.toString(),userInfo);
            voucherMerge.put("master", voucherMaster);
            String r = voucherService.generateVoucher(voucherMerge);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
                Boolean success =voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, id, VoucherType.INVOICE_VERIFY);
                if (!success)
                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        return SUCCESS;
    }

    @Action(value = "revoke_voucher_json",
            results = {@Result(name = "success", location = "/WEB-INF/content/invoice_verify/response_json.ftl")})
    public String revokeVoucher() {
        try {
            voucherService.beginRevokeVoucher(id, VoucherType.BANK_LOAN);
            userInfo = (HashMap<String, Object>) this.session.get(AuthInterceptor.SESSION_KEY);
            Map<String, Object> msgheader = voucherService.getMsgHeader(userInfo);
            Map<String, Object> revokeInfo = bankLoanService.getRevokeInfo(id,xxtype);
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            String r = voucherService.revokeVoucher(info);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                voucherService.revokeVoucherSuccess(id, VoucherType.BANK_LOAN);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        return SUCCESS;
    }

    @Autowired
    public void setBankLoanService(BankLoanService bankLoanService) {
        this.bankLoanService = bankLoanService;
    }

    @Autowired
    public void setVoucherService(VoucherService voucherService) {
        this.voucherService = voucherService;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getVoucherNum() {
        return voucherNum;
    }

    public void setVoucherNum(String voucherNum) {
        this.voucherNum = voucherNum;
    }

    public Integer getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Integer companyId) {
        this.companyId = companyId;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Page<Object[]> getGridResult() {
        return gridResult;
    }

    public void setGridResult(Page<Object[]> gridResult) {
        this.gridResult = gridResult;
    }

    public Date getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(Date systemDate) {
        this.systemDate = systemDate;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getXxtype() {
        return xxtype;
    }

    public void setXxtype(Integer xxtype) {
        this.xxtype = xxtype;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public Map<String, Object> getVoucherMerge() {
        return voucherMerge;
    }

    public void setVoucherMerge(Map<String, Object> voucherMerge) {
        this.voucherMerge = voucherMerge;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }
}
